package gases.as3.librarys.components.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	import gases.as3.collection.utils.ScaleBitmap;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.ImageSkin;
	
	/**
	 * 皮肤盒子
	 * @author gases
	 * 
	 */
	public class GSkinBox extends GUIComponents
	{
		public function GSkinBox()
		{
		}
		
		override protected function initialization():void
		{
			super.initialization();
			if(!_imageBmp)
			{
				_imageBmp = new ScaleBitmap(new BitmapData(10,10,true,0x00000000));
				this.addChild(_imageBmp);
			}
		}
		
		override public function set skin(value:BaseSkin):void
		{
			super.skin = value;
			_imageSkin = value as ImageSkin;
			source = _imageSkin.image;
		}
		
		public function drawColorBackground(w:Number,h:Number,color:uint=0x70b2ee,alpha:Number=1):void
		{
			_type = "color";
			_colorWidth = w;
			_colorHeight = h;
			_color = color;
			_alpha = alpha;
			this.graphics.clear();
			this.graphics.beginFill(color,alpha);
			this.graphics.drawRect(0,0,w,h);
			this.graphics.endFill();
		}
		
		public function set source(value:Object):void
		{
			_type = "image";
			_source = value;
			resloveSource();
		}
		
		public function get source():Object
		{
			return _source;
		}
		
		private function changeBmd(bmd:BitmapData):void
		{
			this.graphics.clear();
			_imageBmp.bitmapData = bmd;
			setWH();
		}
		
		private function resloveSource():void
		{
			if(_source is BitmapData)
			{
				var bmd:BitmapData = _source as BitmapData;
				changeBmd(bmd);
				return;
			}
			if(_source is Bitmap)
			{
				var bmp:Bitmap = _source as Bitmap;
				changeBmd(bmp.bitmapData);
				_imageBmp.scale9Grid = (source as ScaleBitmap).scale9Grid;
				return;
			}
			if(_source is String)
			{
				trace("需要去加载");
				return;
			}
		}
		
		public function get imageBmp():ScaleBitmap
		{
			return _imageBmp;
		}
		
		public function getType():String
		{
			return _type;
		}
		
		public function setWidth(value:Number):void
		{
			if(_type == "color")
			{
				_colorWidth = value;
				this.graphics.clear();
				this.graphics.beginFill(_color,_alpha);
				this.graphics.drawRect(0,0,value,_colorHeight);
				this.graphics.endFill();
			}
			else
			{
				_imageBmp.width = value;
			}
			Gwidth = value;
		}
		
		public function setHeight(value:Number):void
		{
			if(_type == "color")
			{
				_colorHeight = value;
				this.graphics.clear();
				this.graphics.beginFill(_color,_alpha);
				this.graphics.drawRect(0,0,_colorWidth,value);
				this.graphics.endFill();
			}
			else
			{
				_imageBmp.height = value;
			}
			Gheight = value;
		}
		
		private function setWH():void
		{
			if(_type == "color")
			{
				this.graphics.clear();
				this.graphics.beginFill(_color,_alpha);
				this.graphics.drawRect(0,0,Gwidth,Gheight);
				this.graphics.endFill();
			}
			else
			{
				_imageBmp.width = Gwidth;
				_imageBmp.height = Gheight;
			}
			
		}
		
		override public function dispose():void
		{
			if(_imageBmp)
			{
				this.removeChild(_imageBmp);
				if(_imageBmp.bitmapData)
				{
					_imageBmp.bitmapData = null;
				}
				_imageBmp = null;
			}
			this.graphics.clear();
			super.dispose();
		}
		
		private var _source:Object;
		private var _imageBmp:ScaleBitmap;
		private var _isAutoLayout:Boolean;// 是否当图片设置完后按照图片的大小自动布局,暂时无用
		
		private var _imageSkin:ImageSkin;
		
		private var _type:String;
		private var _colorWidth:Number;
		private var _colorHeight:Number;
		private var _color:uint;
		private var _alpha:Number;
	}
}
